/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.viewer;

import com.lucidhex.LucidBoard;
import com.lucidhex.hex.LucidCoord;
import com.lucidhex.hex.LucidIndex;
import com.lucidhex.LucidWorld;
import com.lucidhex.hex.PlaneCoord;
import com.lucidhex.hex.LucidHex;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JPanel;
import javax.swing.JTextField;

  /* Helpful references: 
http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1
http://weblogs.java.net/blog/malenkov/archive/2009/02/hexagonal_tile.html
http://www.tonypa.pri.ee/tbw/tut25.html
	 */
public final class ViewPortal extends JPanel
{		
    private LucidWorld world;
    private LucidBoard board;
    private ViewRenderer pencil = null;
    private ViewOptions vo = null;
    private LucidHex selected = null;
    private ViewMouseListener ml = new ViewMouseListener(this);
    private JTextField out;
    public ViewPortal(){
        this.addMouseListener(ml);
        this.addMouseMotionListener(ml);
        this.addMouseWheelListener(ml);
    }

    public ViewPortal(LucidWorld erf) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void paintComponent(Graphics g){
        if(world == null){
            drawInitalScreen(g, "No World Loaded");
        }
        else if(board.isEmpty()){
            drawInitalScreen(g, "Empty Board");
        }else{
            drawInitalScreen(g, null);
            vo.update();
            pencil.updatePencil(g);
            board.paint(pencil);
            pencil.drawSelectedHex(selected);
        }
    }
    public ViewOptions getViewOptions(){
        return vo;
    }
    public void drawInitalScreen(Graphics gParent, String text){
        Graphics g = gParent.create();
        g.setColor(getBackground());
        g.fillRect(0,0,getSize().width, getSize().height);
        if(text != null){
            g.setColor(ViewRenderer.COLOURTEXT);
            g.drawString(text, getSize().width/3, getSize().height/3);            
        }

    }
    public LucidHex getHexAtPixel(Point p) {
        PlaneCoord sc = vo.revTransform(p);
        LucidIndex ei = new LucidIndex(new LucidCoord(sc));
        return board.getHexAt(ei);
    }

    public void setSelectedHex(LucidHex hexAt) {
        selected = hexAt;
        repaint();
    }

    public void setErf(LucidWorld erfIn) {
        world = erfIn;
        board = world.getBoard();
        vo = new ViewOptions(this, board);
        pencil = new ViewRenderer(this, vo, world);
        setBackground(ViewRenderer.COLOURBACK);
        ml.setErf(world); 
        repaint();
    }
    public void setTextOut(JTextField out){
        this.out = out;
    }
    public void out(String s){
        if(out == null){
            System.out.println(s);
        }else{
            out.setText(s);
        }
    }
    public void rotate30Deg() {
        vo.rotate30Deg();
    }

    boolean isWorldLoaded() {
        if(world != null){
            return !world.getBoard().isEmpty();
        }
        return false;
    }
}